package org.example.project.bookstore.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.project.bookstore.service.ShoppingCartService;
import org.example.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther:Liu
 * @Testname:ShoppingCartController
 * @Date:2025/8/19 18:01
 */
@RestController
@RequestMapping("/ShoppingCart")
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add/{bookId}")
    JsonResult addBook(@PathVariable("bookId") String bookId, HttpServletRequest request) {
        return shoppingCartService.addBook(bookId, request.getHeader("token"));
    }


    @GetMapping("/getMyCart")
    JsonResult findCartByUserId(HttpServletRequest request) {
        return shoppingCartService.findCartByUserId(request);
    }


    @PostMapping("/addOrDelete/{bookId}/{number}")
    JsonResult incrementOrDeleteBookNumber(@PathVariable("bookId")String bookId, @PathVariable("number")int number, HttpServletRequest request) {
        return shoppingCartService.incrementOrDeleteBookNumber(bookId, number, request.getHeader("token"));
    }

    @PutMapping("/update/{bookId}/{number}")
    JsonResult updateBookNumber(@PathVariable("bookId")String bookId, @PathVariable("number")int number, HttpServletRequest request) {
        return shoppingCartService.updateBookNumber(bookId, number, request.getHeader("token"));
    }


    @DeleteMapping("/deleteOne/{bookId}")
    JsonResult deleteBook(@PathVariable("bookId")String bookId, HttpServletRequest request) {
        return shoppingCartService.deleteBook(bookId, request.getHeader("token"));
    }

    @PostMapping("/deleteMore")
    JsonResult deleteMoreBook(String[]bookIds, HttpServletRequest request) {
        return shoppingCartService.deleteMoreBook(bookIds, request.getHeader("token"));
    }
}
